﻿using DomainModel;
using EmitMapper;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;
using UI.JsonModel;
using Common.Extensions.Where;
using Common.Extensions.OrderBy;
using Webdiyer.WebControls.Mvc;

namespace WebPlatform.Controllers
{
    [CustomAuthorize]
    public class CompanyController : Controller
    {

        public IManagerCompany IMCompany { get; set; }


        public IManagerAlbum IMAlbum { get; set; }


        public IManagerPhoto IMPhoto { get; set; }


        public Sessions Sessions { get; set; }

        public ActionResult Index()
        {
            return View();
        }




        /// <summary>
        /// 基础信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Infomation()
        {
            var companyID = Sessions.User.CompanyId;
            var company = IMCompany.GetCompany(companyID);

            var companyVM = ObjectMapperManager
                                .DefaultInstance
                                .GetMapper<CompanyInfo, CompanyVM>()
                                .Map(company);

            return View(companyVM);
        }


        /// <summary>
        /// 更新基础信息
        /// </summary>
        /// <param name="company">基础信息</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Infomation(CompanyVM company)
        {
            var model = IMCompany.GetCompany(company.CompanyID);
            model.Address = company.Address;
            model.Email = company.Email;
            model.Fax = company.Fax;
            model.Name = company.Name;
            model.Phone = company.Phone;
            model.PublishNumber = company.PublishNumber;
            model.Intro = company.Intro;
            model.ContactName = company.ContactName;
            var flag = IMCompany.UpdateCompany(model);
            return Json(flag);
        }



        /// <summary>
        /// 企业介绍
        /// </summary>
        /// <returns></returns>
        public ActionResult Introduction()
        {
            var companyID = Sessions.User.CompanyId;
            var company = IMCompany.GetCompany(companyID);
            var model = ObjectMapperManager
                            .DefaultInstance
                            .GetMapper<CompanyInfo, CompanyVM>()
                            .Map(company);
            return View(model);
        }


        /// <summary>
        /// 企业介绍
        /// </summary>
        /// <param name="companyVM">介绍信息</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Introduction(CompanyVM companyVM)
        {
            var company = IMCompany.GetCompany(companyVM.CompanyID);
            company.Introduction = companyVM.Introduction;
            var flag = IMCompany.UpdateCompany(company);
            return Json(flag);
        }



        /// <summary>
        /// 企业文化
        /// </summary>
        /// <returns></returns>
        public ActionResult Culture()
        {
            var companyID = Sessions.User.CompanyId;
            var company = IMCompany.GetCompany(companyID);
            var model = ObjectMapperManager
                            .DefaultInstance
                            .GetMapper<CompanyInfo, CompanyVM>()
                            .Map(company);
            return View(model);
        }


        /// <summary>
        /// 企业文化
        /// </summary>
        /// <param name="companyVM">文化信息</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Culture(CompanyVM companyVM)
        {
            var company = IMCompany.GetCompany(companyVM.CompanyID);
            company.Culture = companyVM.Culture;
            var flag = IMCompany.UpdateCompany(company);
            return Json(flag);
        }


        public ActionResult Media()
        {
            var companyID = Sessions.User.CompanyId;
            var company = IMCompany.GetCompany(companyID);
            var model = ObjectMapperManager
                            .DefaultInstance
                            .GetMapper<CompanyInfo, CompanyVM>()
                            .Map(company);
            return View(model);
        }

        [HttpPost]
        public ActionResult Media(CompanyVM companyVM)
        {
            var company = IMCompany.GetCompany(companyVM.CompanyID);
            company.MediaUrl = companyVM.MediaUrl;
            var flag = IMCompany.UpdateCompany(company);
            return Json(flag);
        }



        /// <summary>
        /// 联系方式
        /// </summary>
        /// <returns></returns>
        public ActionResult Contact()
        {
            var companyID = Sessions.User.CompanyId;
            var company = IMCompany.GetCompany(companyID);
            var model = ObjectMapperManager
                            .DefaultInstance
                            .GetMapper<CompanyInfo, CompanyVM>()
                            .Map(company);
            return View(model);
        }


        /// <summary>
        /// 联系方式
        /// </summary>
        /// <param name="companyVM">联系方式</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Contact(CompanyVM companyVM)
        {
            var company = IMCompany.GetCompany(companyVM.CompanyID);
            company.Contact = companyVM.Contact;
            var flag = IMCompany.UpdateCompany(company);
            return Json(flag);
        }



        /// <summary>
        /// 企业相册
        /// </summary>
        /// <returns></returns>
        public ActionResult Albums()
        {
            return View();
        }



        /// <summary>
        /// 企业相册
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Albums(int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;
            GridData<AlbumVM> data = new GridData<AlbumVM>();
            var where = PredicateExtension.True<AlbumInfo>();
            where = where.And(u => u.CompanyId == user.CompanyId);
            var count = IMAlbum.LoadCount(where);
            var list = IMAlbum.LoadAll(where, page, count,
                new OrderByExpression<AlbumInfo, int>(u => u.AlbumId, true));


            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<AlbumInfo, AlbumVM>();

            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public ActionResult Album(int aid = 0)
        {
            if (aid > 0)
            {
                var ablumInfo = IMAlbum.GetAlbum(aid);
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<AlbumInfo, AlbumVM>();
                var ablumVM = mapper.Map(ablumInfo);
                return View(ablumVM);
            }
            else
            {
                return View(new AlbumVM() { AlbumId=0 });
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Album(AlbumVM album)
        {
            bool flag = false;
            if (ModelState.IsValid)
            {
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<AlbumVM, AlbumInfo>();

                var albumInfo = mapper.Map(album);
                albumInfo.CompanyId = Sessions.User.CompanyId;
                if (album.AlbumId == 0)
                {
                    flag = IMAlbum.AddAlbum(albumInfo);
                }
                else
                {
                    flag = IMAlbum.UpdateAlbum(albumInfo);
                }
            }

            return Json(flag);
        }


        /// <summary>
        /// 删除相册
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteAlbums(string ids)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var s in idList)
                {
                    list.Add(Convert.ToInt32(s));
                }
                b = IMAlbum.DeleteAlbum(list);
            }
            return Json(b);
        }



        public ActionResult Photos(int aid = 0, int page = 1, int pageSize = 15)
        {
            ViewBag.AlbumId = aid;


            var p = PredicateExtension.True<PhotoInfo>();

            p = p.And(u => u.AlbumId == aid);
            
            var count = IMPhoto.LoadCount(p);
            var list = IMPhoto.LoadAll(p, page, pageSize,
                new OrderByExpression<PhotoInfo, DateTime>(u => u.UploadDate,true));
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PhotoInfo, PhotoVM>();
            var listVM = mapper.MapEnum(list);
            PagedList<PhotoVM> plist =
                new PagedList<PhotoVM>(listVM, page, pageSize, count);

            
            return View(plist);

        }




        public ActionResult Photo(int aid = 0 ,int pid = 0) {
            if (pid > 0)
            {
                var photoInfo = IMPhoto.GetPhoto(pid);
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PhotoInfo, PhotoVM>();
                var photoVM = mapper.Map(photoInfo);
                photoVM.AlbumId = aid;
                if (string.IsNullOrEmpty(photoVM.PhotoUrl))
                {
                    photoVM.PhotoUrl = "/images/nothing.jpg";
                }
                return View(photoVM);
            }
            else
            {
                return View(new PhotoVM() { PhotoId = 0, AlbumId = aid, PhotoUrl = "/images/nothing.jpg" });
            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Photo(PhotoVM photo)
        {
            bool flag = false;
            if (ModelState.IsValid)
            {
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PhotoVM, PhotoInfo>();

                var photoInfo = mapper.Map(photo);

                if (photoInfo.PhotoId == 0)
                {
                    photoInfo.UploadDate = DateTime.Now;
                    flag = IMPhoto.AddPhoto(photoInfo);
                }
                else
                {
                    flag = IMPhoto.UpdatePhoto(photoInfo);
                }
            }

            return Json(flag);
        }


        /// <summary>
        /// 删除相册
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeletePhotos(string ids)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var s in idList)
                {
                    list.Add(Convert.ToInt32(s));
                }
                b = IMPhoto.DeletePhoto(list);
            }
            return Json(b);
        }



    }
}
